/**
 * 获取未读消息接口
 */

//引入Q模块
let Q = require('q');
//存放商户数据库的变量
let db;

let index = function(getData, postData, config, _redis, _db) {
	let deferred = Q.defer();
	db = _db;
	let noDoneOrders = [];
	let noReceiveFunLogs = [];
	db.select('storeOrder', {
		orderDone: false,
		userPayed: true
	}).then(function(_noDoneOrders) {
		noDoneOrders = _noDoneOrders;
		return db.select('noReceiveFunLogs');
	}, function() {
		deferred.reject('-0102');
	}).then(function(_noReceiveFunLogs) {
		noReceiveFunLogs = _noReceiveFunLogs;
		return db.removeAll('noReceiveFunLogs', {});
	}, function() {
		deferred.reject('-0100');
	}).then(function() {
		return db.select('chatListLogs', {
			serviceReceive: false
		});
	}, function() {
		deferred.reject('-0101');
	}).then(function(unreadMsgs) {
		db.updateAll('chatListLogs', {
			serviceReceive: false
		}, {
			serviceReceive: true
		}).then(function() {
			deferred.resolve({
				unreadMsgs: unreadMsgs,
				noDoneOrders: noDoneOrders,
				noReceiveFunLogs: noReceiveFunLogs
			});
		}, function() {
			deferred.reject('-0062');
		});
	}, function() {
		deferred.reject('-0062');
	});
	return deferred.promise;
}

module.exports = {
	index: index
};